package com.example.controller;

import com.example.annotation.DecryptRequestBody;
import com.example.annotation.EncryptResponseBody;
import com.example.dto.Request;
import com.example.dto.Response;
import com.example.dto.VehicleRequestDTO;
import com.example.dto.VehicleResponseDTO;
import com.example.service.DemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/demo")
public class DemoController {

    private static final Logger logger = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private DemoService demoService;

    @GetMapping("/hello")
    public String hello() {
        logger.info("Handling hello request");
        return demoService.sayHello();
    }

    @PostMapping("/test")
    public String test(@RequestBody String requestBody) {
        logger.info("Handling test request with body: {}", requestBody);
        return demoService.processRequestBody(requestBody);
    }

    /**
     * 直接在请求头中进行配置，使用过滤器进行解密和加密
     * （更改请求头解密加密参数即可，比较灵活方便，过滤器只是解密成json格式的字符串，但是并不完成request的注入功能，因此依然需要RequestBody注解将json转化为对象）
     * 处理车辆信息的请求，并返回 JSON 格式的响应
     *
     * @param request 包含车辆信息的请求体
     * @return 处理结果的响应
     */
    @PostMapping("/vehicle")
    public Response<VehicleResponseDTO> processVehicleRequest(@RequestBody Request<VehicleRequestDTO> request) {
        logger.info("Handling vehicle request with vehicleId: {}", request.getData().getVehicleId());
        VehicleResponseDTO vehicleResponse = demoService.processVehicle(request.getData());
        return Response.success(vehicleResponse);
    }

    /**
     * 使用切面解密加密，请求体加密后不再支持RequestBody注解，切面直接完成了RequestBody注入对象的功能，加入RequestBody会重复报错，而且contentType也无法兼容
     */
    @DecryptRequestBody
    @EncryptResponseBody
    @PostMapping("/vehicle2")
    public Response<VehicleResponseDTO> processVehicleRequest2(Request<VehicleRequestDTO> request) {
        logger.info("Handling vehicle request with vehicleId: {}", request.getData().getVehicleId());
        VehicleResponseDTO vehicleResponse = demoService.processVehicle(request.getData());
        return Response.success(vehicleResponse);
    }
}
